    #include "commonpage.h"
#include "ui_commonpage.h"
#include <QSize>
#include <QListWidgetItem>

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);

    // 不要⽔平滚动条
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    // playAllBtn按钮的信号槽处理
    // 当播放按钮点击时，发射playAll信号，播放当前⻚⾯的所有歌曲
    // playAll信号交由QQMusic中处理
    connect(ui->playAllBtn,&QPushButton::clicked,this,[=](){
        emit playAll(_pageType);
    });

    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex& index){
        // ⿏标双击后，发射信号告诉Music，播放this⻚⾯中共被双击的歌曲
        emit playMusicByIndex(this,index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUi(const QString &title, const QString &image)
{
    // 设置标题
    ui->PageTitle->setText(title);

    // 设置封⾯栏
    ui->musicImageLabel->setPixmap(QPixmap(image));
    //设置图片缩放
    ui->musicImageLabel->setScaledContents(true);

    // 测试
//    ListItemBox* listitembox = new ListItemBox(this);
//    QListWidgetItem* listwidgetitem = new QListWidgetItem(ui->pageMusicList);
//    listwidgetitem->setSizeHint(QSize(ui->pageMusicList->width(),45));
//    ui->pageMusicList->setItemWidget(listwidgetitem,listitembox);
}

void CommonPage::setMusicListType(PageType pageType)
{
    this->_pageType = pageType;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    // 将旧内容清空
    _musicListOfPage.clear();

    for(auto& music : musicList)
    {
        switch(_pageType)
        {
        case LOCAL_PAGE :
            _musicListOfPage.push_back(music.getMusicId());
            break;
        case LIKE_PAGE :
            if(music.getIsLike())
            {
                _musicListOfPage.push_back(music.getMusicId());
            }
            break;
        case HISTORY_PAGE :
            if(music.getIsHistory())
            {
                _musicListOfPage.push_back(music.getMusicId());
            }
            break;
        default:
            break;
        }
    }
}

void  CommonPage::reFresh(MusicList &musicList)
{
    // 将QListWidget之前已经添加的内容清空
    ui->pageMusicList->clear();

    // 从musicList中分离出当前⻚⾯的所有⾳乐
    addMusicToMusicPage(musicList);

    // 遍历歌单，将歌单中的歌曲显⽰到界⾯
    for(auto musicId : _musicListOfPage)
    {
        auto it = musicList.findMusicById(musicId);
        if(it == musicList.end())
            continue;

        ListItemBox* listItemBox = new ListItemBox(ui->pageMusicList);
        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setSinger(it->getSingerName());
        listItemBox->setAlbumName(it->getAlbumName());
        listItemBox->setLikeIcon(it->getIsLike());

        QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
        listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(),45));
        ui->pageMusicList->setItemWidget(listWidgetItem,listItemBox);

        // 接收ListItemBox发射的setLikeMusic信号
        connect(listItemBox,&ListItemBox::setIsLike,this,[=](bool isLike){
            emit updateLikeMusic(isLike,it->getMusicId());
        });
    }

    // 更新完成后刷新下界⾯
    // 该函数最后添加上repaint()函数调⽤
    // repaint()会⽴即执⾏paintEvent()，不会等待事件队列的处理
    // update（）将⼀个paintEvent事件添加到事件队列中，等待稍后执⾏，即不会⽴即执⾏
    repaint();
}

void CommonPage::addMusicToPlayList(MusicList &musicList, QMediaPlaylist *playerList)
{
//    qDebug() << "addMusicToPlayList";
    // 根据⾳乐列表中⾳乐所属的⻚⾯，将⾳乐添加到playList中
    for(auto music : musicList)
    {
        switch(_pageType)
        {
            case LOCAL_PAGE:
                playerList->addMedia(music.getMusicUrl());
                break;
            case LIKE_PAGE:
                if(music.getIsLike())
                {
                    playerList->addMedia(music.getMusicUrl());
                }
                break;
            case HISTORY_PAGE:
                if(music.getIsHistory())
                {
                    playerList->addMedia(music.getMusicUrl());
                }
                break;
             default:
                break;
        }
    }
}

const QString &CommonPage::getMusicIDByIndex(int index) const
{
    if(index >= _musicListOfPage.size())
    {
        qDebug() << "无此歌曲";
        return "";
    }
    return _musicListOfPage[index];
}

void CommonPage::setImageLable(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}
